/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sys.screen;

import java.sql.SQLException;
import java.util.List;
import sys.bean.CidadeBean;
import sys.bean.EstadoBean;
import sys.bean.MotoristaBean;
import sys.dao.CidadeDao;
import sys.dao.EstadoDao;
import sys.dao.MotoristaDao;
import sys.utils.LetterUpper;
import sys.utils.Mensagens;
import sys.utils.Utils;

/**
 *
 * @author Administrador
 */
public class CCadastroMotorista extends javax.swing.JDialog {

    List<CidadeBean> listaCidades;
    List<EstadoBean> listaEstados;

    CidadeDao daoCidade = new CidadeDao();
    EstadoDao daoEstado = new EstadoDao();
    MotoristaDao daoMotorista = new MotoristaDao();
    MotoristaBean motorista;

    private boolean inserir;
    private CPesquisaMotorista telaPesquisaMotorista;

    /**
     * Creates new form CCadastroEmpresa
     *
     * @param parent
     * @param modal
     */
    public CCadastroMotorista(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        Utils.escClose(this);
        atualizaEstados();
    }

    public CCadastroMotorista(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        atualizaEstados();
        telaPesquisaMotorista = (CPesquisaMotorista) parent;
    }

    public void carregaMotorista(int id) {
        try {
            motorista = daoMotorista.consultaMotorista(id);

            txCodigo.setText(motorista.getIdMotorista() + "");
            txCpf.setText(motorista.getCpf());
            txRG.setText(motorista.getRg());
            txNome.setText(motorista.getNome());
            txSobreNome.setText(motorista.getSobreNome());
            txObs.setText(motorista.getObs());

            cbEstado.getModel().setSelectedItem(motorista.getCidade().getEstado().getNome() + "-" + motorista.getCidade().getEstado().getUf());
            cbCidade.getModel().setSelectedItem(motorista.getCidade().getNome() + "-" + motorista.getCidade().getEstado().getUf());

        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao consultar dados da empresa\n" + ex.getMessage());
        }

    }

    private MotoristaBean retornaObjeto() {

        MotoristaBean m = new MotoristaBean();
        CidadeBean cidade = new CidadeBean();

        if (!isInserir()) {
            m.setIdMotorista(Integer.parseInt(txCodigo.getText()));
        }

        cidade.setIdCidage(listaCidades.get(cbCidade.getSelectedIndex() - 1).getIdCidage());
        m.setCidade(cidade);
        m.setNome(txNome.getText());
        m.setSobreNome(txSobreNome.getText());
        m.setCpf(txCpf.getText());
        m.setRg(txRG.getText());
        m.setObs(txObs.getText());

        return m;
    }

    private void atualizaEstados() {
        cbEstado.removeAllItems();
        try {
            listaEstados = daoEstado.listaEstados();
            cbEstado.addItem("<< SELECIONE ESTADO >>");
            for (EstadoBean lEstados : listaEstados) {
                cbEstado.addItem(lEstados.getNome() + "-" + lEstados.getUf());
            }
        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao atualizar estados\n" + ex.getMessage());
        }
    }

    private void atualizaCidades() {
        cbCidade.removeAllItems();
        try {
            listaCidades = daoCidade.listaCidades(listaEstados.get(cbEstado.getSelectedIndex() - 1).getIdEstado());
            cbCidade.addItem("<< SELECIONE CIDADE >>");
            for (CidadeBean lCidades : listaCidades) {
                cbCidade.addItem(lCidades.getNome() + "-" + lCidades.getUf());
            }
        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao atualizar cidades\n" + ex.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txSobreNome = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txRG = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        cbEstado = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        cbCidade = new javax.swing.JComboBox();
        txCpf = new javax.swing.JFormattedTextField();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txObs = new javax.swing.JTextArea();
        jPanel7 = new javax.swing.JPanel();
        btnSalvar4 = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("MANUTENÇÃO DE CADASTRO");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DADOS DA EMPRESA:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 10))); // NOI18N
        jPanel1.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel1.setText("CODIGO:");

        txCodigo.setEditable(false);
        txCodigo.setBackground(new java.awt.Color(245, 245, 245));
        txCodigo.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txCodigo.setForeground(new java.awt.Color(51, 51, 255));
        txCodigo.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel2.setText("NOME:");

        txNome.setDocument(new LetterUpper());
        txNome.setBackground(new java.awt.Color(245, 245, 245));
        txNome.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txNome.setForeground(new java.awt.Color(51, 51, 255));

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel3.setText("SOBRENOME:");

        txSobreNome.setDocument(new LetterUpper());
        txSobreNome.setBackground(new java.awt.Color(245, 245, 245));
        txSobreNome.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txSobreNome.setForeground(new java.awt.Color(51, 51, 255));

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel4.setText("CPF:");

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel5.setText("RG:");

        txRG.setBackground(new java.awt.Color(245, 245, 245));
        txRG.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txRG.setForeground(new java.awt.Color(51, 51, 255));
        txRG.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel9.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel9.setText("ESTADO:");

        cbEstado.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        cbEstado.setForeground(new java.awt.Color(51, 51, 255));
        cbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<< SELECIONE ESTADO >>" }));
        cbEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbEstadoActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel10.setText("CIDADE:");

        cbCidade.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        cbCidade.setForeground(new java.awt.Color(51, 51, 255));
        cbCidade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<< SELECIONE CIDADE >>" }));

        txCpf.setBackground(new java.awt.Color(245, 245, 245));
        txCpf.setForeground(new java.awt.Color(51, 51, 255));
        try {
            txCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txCpf.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txCpf.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        jLabel13.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel13.setText("OBSERVAÇÕES:");

        txObs.setDocument(new LetterUpper());
        txObs.setColumns(20);
        txObs.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        txObs.setForeground(new java.awt.Color(51, 51, 255));
        txObs.setLineWrap(true);
        txObs.setRows(5);
        jScrollPane1.setViewportView(txObs);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbCidade, 0, 225, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(txCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(txNome, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(txSobreNome)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel4)
                            .addComponent(txCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel10))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txRG))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txSobreNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txRG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addGap(1, 1, 1)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbCidade))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32))
        );

        jPanel7.setBackground(new java.awt.Color(245, 245, 245));
        jPanel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));

        btnSalvar4.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnSalvar4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/salvar.png"))); // NOI18N
        btnSalvar4.setText("SALVAR");
        btnSalvar4.setPreferredSize(new java.awt.Dimension(120, 30));
        btnSalvar4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvar4ActionPerformed(evt);
            }
        });
        jPanel7.add(btnSalvar4);

        btnSair.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/sair.png"))); // NOI18N
        btnSair.setText("SAIR");
        btnSair.setPreferredSize(new java.awt.Dimension(120, 30));
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        jPanel7.add(btnSair);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, 470, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvar4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvar4ActionPerformed
        try {
            if (isInserir()) {
                daoMotorista.incluir(retornaObjeto());
                telaPesquisaMotorista.atualizaMotoristas();
                this.dispose();
            } else {
                daoMotorista.alterar(retornaObjeto());
                telaPesquisaMotorista.atualizaMotoristas();
                this.dispose();
            }
        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao cadastrar empresa\n" + ex.getMessage());
        }
    }//GEN-LAST:event_btnSalvar4ActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void cbEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbEstadoActionPerformed
        try {
            atualizaCidades();
        } catch (java.lang.NullPointerException | java.lang.ArrayIndexOutOfBoundsException e) {

        }
    }//GEN-LAST:event_cbEstadoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnSalvar4;
    private javax.swing.JComboBox cbCidade;
    private javax.swing.JComboBox cbEstado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txCodigo;
    private javax.swing.JFormattedTextField txCpf;
    private javax.swing.JTextField txNome;
    private javax.swing.JTextArea txObs;
    private javax.swing.JTextField txRG;
    private javax.swing.JTextField txSobreNome;
    // End of variables declaration//GEN-END:variables

    public boolean isInserir() {
        return inserir;
    }

    public void setInserir(boolean inserir) {
        this.inserir = inserir;
    }
}
